package com.aidex.biz.controller;

import com.aidex.biz.domain.JinjianBillDetails;
import com.aidex.biz.service.JinjianBillDetailsService;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 费用明细Controller
 * @author sean
 * @email 2074844820@qq.com
 * @date 2023-12-17
 */
@RestController
@RequestMapping("/biz/bizJinjianBillDetails")
public class JinjianBillDetailsController extends BaseController
{
    @Autowired
    private JinjianBillDetailsService jinjianBillDetailsService;

    /**
     * 查询费用明细列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizJinjianBillDetails:list')")
    @GetMapping("/list")
    public R<PageInfo> list(JinjianBillDetails jinjianBillDetails, HttpServletRequest request, HttpServletResponse response)
    {
        if(SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                jinjianBillDetails.setCompanyId(null);
        }else{
                     jinjianBillDetails.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
                 }
        jinjianBillDetails.setPage(new PageDomain(request, response));
        return R.data(jinjianBillDetailsService.findPage(jinjianBillDetails));
    }

    /**
     * 获取费用明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizJinjianBillDetails:query')")
    @GetMapping(value = "/{id}")
    public R<JinjianBillDetails> detail(@PathVariable("id") String id)
    {
        return R.data(jinjianBillDetailsService.get(id));
    }

    /**
     * 新增费用明细
     */
    @PreAuthorize("@ss.hasPermi('biz:bizJinjianBillDetails:add')")
    @Log(title = "费用明细", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  JinjianBillDetails jinjianBillDetails)
    {
        return R.status(jinjianBillDetailsService.save(jinjianBillDetails));
    }

    /**
     * 修改费用明细
     */
    @PreAuthorize("@ss.hasPermi('biz:bizJinjianBillDetails:edit')")
    @Log(title = "费用明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated JinjianBillDetails jinjianBillDetails)
    {
        return R.status(jinjianBillDetailsService.save(jinjianBillDetails));
    }


    /**
     * 删除费用明细
     */
    @PreAuthorize("@ss.hasPermi('biz:bizJinjianBillDetails:remove')")
    @Log(title = "费用明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids)
    {
        return R.status(jinjianBillDetailsService.deleteJinjianBillDetailsByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('biz:bizJinjianBillDetails:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort()
    {
        return R.data(jinjianBillDetailsService.findMaxSort(new JinjianBillDetails()));
    }

    /**
     * 导出费用明细列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizJinjianBillDetails:export')")
    @Log(title = "费用明细", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(JinjianBillDetails jinjianBillDetails)
    {

            if(SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                    jinjianBillDetails.setCompanyId(null);
            }else{
                         jinjianBillDetails.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
                     }
        List<JinjianBillDetails> list = jinjianBillDetailsService.findList(jinjianBillDetails);
        ExcelUtil<JinjianBillDetails> util = new ExcelUtil<JinjianBillDetails>(JinjianBillDetails.class);
        return util.exportExcel(list, "费用明细数据");
    }

}
